/*
 * @Author: zhangshu
 * @email: shiliang.zhang@tuya.com
 * @LastEditors: Please set LastEditors
 * @file name: app_ias_zone_attribute.c
 * @Description: 
 * @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
 * @Company: http://www.tuya.com
 * @Date: 2022-02-08
 * @LastEditTime: 2022-02-08
 */
#include "tal_memory.h"
#include "tal_attribute_rw.h"

#include "app_common.h"
#include "app_ias_zone_attribute.h"

//invalid zone id
#define IAS_ZONE_INVALID_ZONE_ID            0XFF

//read ias zone attribute
#define read_zone_attribute(aid, value, len) \
    read_sensor_attribute(IAS_ZONE_ENDPOINT, \
                          CLUSTER_IAS_ZONE_CLUSTER_ID, \
                          aid, \
                          (VOID_T *)value, \
                          len)

//write ias zone attribute
#define write_zone_attribute(aid, value, type) \
    write_sensor_attribute(IAS_ZONE_ENDPOINT, \
                           CLUSTER_IAS_ZONE_CLUSTER_ID, \
                           aid, \
                           value, \
                           type)

/**
 * @description: read ias zone state
 * @param[in] none
 * @return: zone state, enroll or not enroll
 */
UINT8_T read_ias_zone_state(VOID_T)
{
    UINT8_T temp = 0;
    OPERATE_RET status = OPRT_COM_ERROR;
    status = read_zone_attribute(ATTR_ZONE_STATE_ATTRIBUTE_ID, &temp, SIZEOF(temp));
    if (OPRT_OK!=status) {
        return INVALID_UINT8_DATA;
    }
    return temp;
}

/**
 * @description: read ias zone type
 * @param[in] none
 * @return: ias zone type or invalid data
 */
UINT16_T read_ias_zone_type(VOID_T)
{
    UINT16_T temp = 0;
    OPERATE_RET status = OPRT_COM_ERROR;
    status = read_zone_attribute(ATTR_ZONE_TYPE_ATTRIBUTE_ID, &temp, SIZEOF(temp));
    if (OPRT_OK!=status) {
        return INVALID_UINT16_DATA;
    }
    return temp;
}

/**
 * @description: read ias zone status
 * @param[in] none
 * @return: ias zone status or invalid data
 */
UINT16_T read_ias_zone_status(VOID_T)
{
    UINT16_T temp = 0;
    OPERATE_RET status = OPRT_COM_ERROR;
    status = read_zone_attribute(ATTR_ZONE_STATUS_ATTRIBUTE_ID, &temp, SIZEOF(temp));
    if (OPRT_OK!=status) {
        return INVALID_UINT16_DATA;
    }
    return temp;
}

/**
 * @description: read ias zone cie address
 * @param[out] cie_addr: ias zone CIE address
 * @return: TRUE when read success or not
 */
BOOL_T read_ias_zone_cie_address(UINT8_T *cie_addr)
{
    OPERATE_RET status = OPRT_COM_ERROR;
    status = read_zone_attribute(ATTR_IAS_CIE_ADDRESS_ATTRIBUTE_ID, cie_addr, 8);
    if (OPRT_OK!=status) {
        return FALSE;
    }
    return TRUE;
}

/**
 * @description: read ias zone id
 * @param[in] none
 * @return: ias zone id or invalid data
 */
UINT8_T read_ias_zone_id(VOID_T)
{
    UINT8_T temp = 0;
    OPERATE_RET status = OPRT_COM_ERROR;
    status = read_zone_attribute(ATTR_ZONE_ID_ATTRIBUTE_ID, &temp, 1);
    if (OPRT_OK!=status) {
        return INVALID_UINT8_DATA;
    }
    return temp;
}

/**
 * @description: set ias zone state after receive enroll response
 * @param[in] enroll_state: zone enroll state
 * @return: none
 */
VOID_T write_ias_zone_state(UINT8_T enroll_state)
{
    write_zone_attribute(ATTR_ZONE_STATE_ATTRIBUTE_ID, &enroll_state, ATTR_ENUM8_ATTRIBUTE_TYPE);
}

/**
 * @description: set ias zone type after receive enroll response
 * @param[in] zone_type: zone type
 * @return: none
 */
VOID_T write_ias_zone_type(UINT16_T zone_type)
{
    write_zone_attribute(ATTR_ZONE_TYPE_ATTRIBUTE_ID, &zone_type, ATTR_ENUM16_ATTRIBUTE_TYPE);
}

/**
 * @description: set ias zone status after receive enroll response
 * @param[in] zone_status: zone type
 * @return: none
 */
VOID_T write_ias_zone_status(UINT16_T zone_status)
{
    write_zone_attribute(ATTR_ZONE_STATUS_ATTRIBUTE_ID, &zone_status, ATTR_BITMAP16_ATTRIBUTE_TYPE);
}

/**
 * @description: set ias zone cie address
 * @param[in] cie_addr: ias zone CIE address
 * @return: none
 */
VOID_T write_ias_zone_cie_address(UINT8_T *cie_addr)
{
    write_zone_attribute(ATTR_IAS_CIE_ADDRESS_ATTRIBUTE_ID, cie_addr, ATTR_IEEE_ADDRESS_ATTRIBUTE_TYPE);
}

/**
 * @description: set ias zone id after receive enroll response
 * @param[in] zone_id: ias zone id
 * @return: none
 */
VOID_T write_ias_zone_id(UINT8_T zone_id)
{
    write_zone_attribute(ATTR_ZONE_ID_ATTRIBUTE_ID, &zone_id, ATTR_INT8U_ATTRIBUTE_TYPE);
}

